package com.qiaotongtianxia.loans.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.ColorInt;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;

import com.qiaotongtianxia.loans.R;
import com.qiaotongtianxia.loans.util.CommonUtils;


/**
 * 带圆角边框的textview
 * Created by Administrator on 2017/9/6.
 * lisen
 */

public class RoundCornerTextView extends BaseTextView {
    public RoundCornerTextView(Context context) {
        this(context,null);
    }

    public RoundCornerTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    private int cornerColor;//外边框的颜色
    private int corner;//外边框的圆角（dp）

    private Paint paint;
    public RoundCornerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundCornerTextView, defStyleAttr, 0);
        cornerColor = typedArray.getColor(R.styleable.RoundCornerTextView_cornerColor, ContextCompat.getColor(context,R.color.mainColor));
        corner = typedArray.getResourceId(R.styleable.RoundCornerTextView_corner, 2);

        corner = CommonUtils.dp2px(context,corner);
        typedArray.recycle();

        paint = new Paint();
        paint.setColor(cornerColor);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(4);
        paint.setAntiAlias(true);

        setTextColor(cornerColor);
    }

    public void setCornerColor(@ColorInt int cornerColor) {
        this.cornerColor = cornerColor;
        paint.setColor(cornerColor);
        setTextColor(cornerColor);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        path = new Path();
        path.moveTo(corner,0);
        path.lineTo(getMeasuredWidth() - corner,0);
        path.quadTo(getMeasuredWidth(),0,getMeasuredWidth(),corner);//右上角

        path.lineTo(getMeasuredWidth(),getMeasuredHeight()-corner);
        path.quadTo(getMeasuredWidth(),getMeasuredHeight(),getMeasuredWidth() - corner,getMeasuredHeight());//右下角
        path.lineTo(corner,getMeasuredHeight());
        path.quadTo(0,getMeasuredHeight(),0,getMeasuredHeight() - corner);//左下角
        path.lineTo(0 ,corner);
        path.quadTo(0,0,corner,0);//左上角
        path.close();

    }

    private Path path;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(path,paint);
    }
}
